﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace blackhouse.BlackApart.Data {
    public abstract class AdoFurnitureDataAccess : IFurnitureDataAccess {

        #region IFurnitureDataAccess Members

        public abstract int SaveFurniture(int id, string name, string description, int sort, string imageName, byte[] imageData);

		public FurnitureData ReadFurniture(int furnitureId, string language) {
            return this.ReadFurniture(this.GetReaderForFurniture(furnitureId, language));
        }

		public FurnitureData[] GetApartmentFurnitures(int apartmentId, string language) {
            return this.ReadFurnitures(this.GetReaderForApartmentFurnitures(apartmentId, language));
        }

		public FurnitureData[] GetApartmentTypeFurnitures(int apartmentTypeId, string language) {
            return this.ReadFurnitures(this.GetReaderForApartmentTypeFurnitures(apartmentTypeId, language));
        }

        public abstract bool AddFurnitureToApartment(int furnitureId, int apartmentId);

        #endregion

        #region Methods

		protected abstract IDataReader GetReaderForFurniture(int furnitureId, string language);

		protected abstract IDataReader GetReaderForApartmentFurnitures(int apartmentId, string language);

		protected abstract IDataReader GetReaderForApartmentTypeFurnitures(int apartmentTypeId, string language);

        #endregion

        #region Methods

        private FurnitureData ReadFurniture(IDataReader reader) {
            if (reader.Read())
                return this.ReadFurnitureFields(reader);
            else return default(FurnitureData);
        }

        private FurnitureData[] ReadFurnitures(IDataReader reader) {
            List<FurnitureData> data = new List<FurnitureData>();
            while (reader.Read())
                data.Add(this.ReadFurnitureFields(reader));
            return data.ToArray();
        }

        private FurnitureData ReadFurnitureFields(IDataReader reader) {
            FurnitureData data = new FurnitureData();
            data.Description = Convert.ToString(reader["Description"]);
            data.Id = Convert.ToInt32(reader["Id"]);
            data.ImageName = Convert.ToString(reader["ImageName"]);
            data.Name = Convert.ToString(reader["Name"]);
            data.Sort = Convert.ToInt32(reader["Sort"]);
            return data;
        }

        #endregion

    }
}
